package com.udl.emailfs.utils;

import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;

public class DynamicTree extends JPanel {

	private static final long serialVersionUID = -747762033276691485L;
	protected DefaultMutableTreeNode rootNode;
	protected DefaultTreeModel treeModel;
	protected JTree tree;
	private Toolkit toolkit = Toolkit.getDefaultToolkit();
	private TreePath selPath;

	public DynamicTree() {
		super(new GridLayout(1, 0));

		rootNode = new DefaultMutableTreeNode(ConstantsFS.SUPER_BLOCK);
		treeModel = new DefaultTreeModel(rootNode);

		tree = new JTree(treeModel);
		tree.setEditable(false);
		tree.getSelectionModel().setSelectionMode(
				TreeSelectionModel.SINGLE_TREE_SELECTION);
		tree.setShowsRootHandles(true);
		
		tree.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				// Se obtiene el path para esa fila. Con el path podemos obtener
				// los nodos.
				selPath = tree.getPathForLocation(e.getX(), e.getY());
			}
		});	
		
		JScrollPane scrollPane = new JScrollPane(tree);
		add(scrollPane);
	}
	
	public String getSelPath(){
		return selPath == null ? null : selPath.getLastPathComponent().toString();
	}

	public void clear() {
		TreePath currentSelection = tree.getSelectionPath();
		DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(currentSelection);
		childNode.removeAllChildren();
		treeModel.reload();
	}
	
	public String getAbsolutePath() {
		TreePath currentSelection = tree.getSelectionPath();
		DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) (currentSelection
				.getLastPathComponent());
		MutableTreeNode parent = (MutableTreeNode) (currentNode.getParent());
		String absolutePath = currentNode.toString();
		
		if(parent != null){
				absolutePath = "/" + absolutePath;
				while(parent != null && !parent.toString().equals(ConstantsFS.SUPER_BLOCK)){
					absolutePath = "/" + parent.toString() + absolutePath;
					parent = (MutableTreeNode) parent.getParent();
				}	
		}
		return absolutePath;
		
	}
	
	public String getRealPath() {
		TreePath currentSelection = tree.getSelectionPath();
		DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) (currentSelection
				.getLastPathComponent());
		MutableTreeNode parent = (MutableTreeNode) (currentNode.getParent());
		String absolutePath = "/";
		
		if(parent != null){
				while(parent != null && !parent.toString().equals(ConstantsFS.SUPER_BLOCK)){
					absolutePath = "/" + parent.toString() + absolutePath;
					parent = (MutableTreeNode) parent.getParent();
				}	
		}
		return absolutePath;
		
	}

	public void removeCurrentNode() {
		TreePath currentSelection = tree.getSelectionPath();
		if (currentSelection != null) {
			DefaultMutableTreeNode currentNode = (DefaultMutableTreeNode) (currentSelection
					.getLastPathComponent());
			MutableTreeNode parent = (MutableTreeNode) (currentNode.getParent());
			if (parent != null) {
				treeModel.removeNodeFromParent(currentNode);
				return;
			}
		}

		toolkit.beep();
	}

	public DefaultMutableTreeNode addObject(Object child) {
		DefaultMutableTreeNode parentNode = null;
		TreePath parentPath = tree.getSelectionPath();

		if (parentPath == null) {
			parentNode = rootNode;
		} else {
			parentNode = (DefaultMutableTreeNode) (parentPath.getLastPathComponent());
		}

		return addObject(parentNode, child);
	}

	public DefaultMutableTreeNode addObject(DefaultMutableTreeNode parent,
			Object child) {
		DefaultMutableTreeNode childNode = new DefaultMutableTreeNode(child);

		if (parent == null) {
			parent = rootNode;
		}

		treeModel.insertNodeInto(childNode, parent, parent.getChildCount());
		tree.scrollPathToVisible(new TreePath(childNode.getPath()));
		
		return childNode;
	}
}